package com.maokeyang.linkedlist;

public class LinkedList<E> {

    /**
     * 定义节点
     */
    private class Node {
        E e;
        Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    // 定义头结点 （适合创建虚拟头结点）
    private Node dummyHead;

    // 定义链表长度
    private int size;

    /**
     * 初始化 一个链表
     */
    public LinkedList() {
        dummyHead = new Node();
        size = 0;
    }

    /**
     * 获取链表的长度
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * 在 index位置 添加元素
     */
    public void add(int index, E e) {

        if (index < 0 || index > size) {
            throw new IllegalArgumentException("add fail");
        }

        Node prev = dummyHead;

        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        prev.next = new Node(e, prev.next);
        size++;
    }


    /**
     *  首位添加元素
     * @param e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 尾部添加元素
     *
     * @param e
     */
    public void addTail(E e) {
        add(size, e);
    }

    /**
     * 更新索引位置为index的 值
     *
     * @param index
     * @param e
     */
    public E set(int index, E e) {

        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("set fail");
        }

        Node cur = dummyHead.next;

        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        Node retNode = cur;
        cur.e = e;
        return retNode.e;
    }

    /**
     * 删除在index位置的 结点
     *
     * @param index
     * @return
     */
    public E del(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("del fail");
        }

        Node prev = dummyHead;

        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;

        size--;

        return retNode.e;
    }

    /**
     * 获取索引为index的结点值
     *
     * @param index
     * @return
     */
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("get fail");
        }

        Node cur = dummyHead.next;

        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        return cur.e;
    }

    /**
     * 判断是不是链表为空
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 链表里是否包含某个元素e
     *
     * @param e
     * @return
     */
    public boolean contain(E e) {
        Node cur = dummyHead.next;

        while (cur != null) {
            if (cur.e.equals(e)) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }


    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();

        Node cur = dummyHead.next;

        res.append("链表打印：");
        while (cur != null) {
            res.append(cur + "-->");
            cur = cur.next;
        }

        res.append("NULL");

        return res.toString();
    }
}
